﻿using System.Linq;
using GC_FFW.BusinessEntities.Abstractions.Interfaces.Forecast;
using GC_FFW.BusinessEntities.Forecast.Xml;
using GC_FFW.ForecastWriter.Abstractions.Interfaces.Helpers;
using System;

namespace GC_FFW.ForecastWriter.Abstractions.Adapters
{
	public abstract class ProposalBaseAdapter
	{
		#region Properties

		protected IForecastData ForecastData { get; private set; }

		protected Conditions Conditions { get; private set; }

		protected IStringManager StringManager { get; private set; }

		protected IRandomizer Randomizer { get; private set; }

		#endregion

		#region Constructors

		protected ProposalBaseAdapter(IForecastData forecastData, Conditions conditions,
			IStringManager stringManager, IRandomizer randomizer)
		{
			if (forecastData == null)
				throw new ArgumentNullException(string.Format("Forecast Data"));
			ForecastData = forecastData;

			if (conditions == null)
				throw new ArgumentNullException(string.Format("Conditions"));
			Conditions = conditions;

			if (stringManager == null)
				throw new ArgumentNullException(string.Format("String Manager"));
			StringManager = stringManager;

			if (randomizer == null)
				throw new ArgumentNullException(string.Format("Randomizer"));
			Randomizer = randomizer;
		}

		#endregion

		#region Virtual Methods

		public virtual string GetText()
		{
			var proposal = GetProposal();
			return proposal != null
				? proposal.Text
				: string.Empty;
		}

		#endregion

		#region Protected Methods

		protected Proposal GetProposal()
		{
			if (Conditions.Items == null || !Conditions.Items.Any()) return null;
			return GetProposal(Conditions.Items.OrderBy(i => i.Order).ElementAt(Randomizer.Next(Conditions.Items.Count)));
		}

		protected Proposal GetProposal(Condition condition)
		{
			return condition != null && condition.Proposals != null && condition.Proposals.Any()
				? condition.Proposals.ElementAt(Randomizer.Next(condition.Proposals.Count))
				: null;
		}

		#endregion
	}
}
